Imports DotNetNuke
Imports DotNetNuke.Entities
Imports System
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Web.Profile
Imports System.Web.HttpRequest
Imports System.Web.HttpContext


Partial Class ACAjaxHelper
    Inherits DotNetNuke.Framework.PageBase
    ' Methods

    Private Shared Function GetLegendResource(ByVal key As String) As String
        Return DotNetNuke.Services.Localization.Localization.GetString(key, "~/DesktopModules/ActiveCompare/App_LocalResources/Legend.resx")
    End Function
    Private Shared Function GetCompareResource(ByVal key As String) As String
        Return DotNetNuke.Services.Localization.Localization.GetString(key, "~/DesktopModules/ActiveCompare/App_LocalResources/Compare.ascx.resx")
    End Function

    Private Shared Function BuildComparison(ByVal ProductGroupId As Integer, ByVal AbsPath As String) As StringBuilder
        Dim output As New StringBuilder
        Dim col_count As Integer = 0
        Dim temp_count As Integer = 0
        Dim list As New ArrayList
        Dim pc As New ProductController
        Dim fc As New FeatureController
        Dim fgc As New FeatureGroupController
        Dim fgi As New FeatureGroupInfo
        Try
            output.Append("<table class=""apc_productcomparison_table "" cellpadding=""0"" cellspacing=""0"">")
            Dim dr As IDataReader = pc.GetProductListByProductGroupId(ProductGroupId)
            output.Append("<tr class=""apc_product_row"">")
            output.Append("<td></td>")
            col_count += 1
            Do While dr.Read
                output.Append("<td class=""apc_product_cell"">")
                output.Append("<span class=""apc_product"">")
                output.Append(Convert.ToString(dr("Product")))
                output.Append("</span")
                output.Append("</td>")
                col_count += 1
            Loop
            output.Append("</tr>")
            dr.Close()
            dr = Nothing
            If (col_count <> 1) Then
                dr = fgc.GetFeatureGroupsByProductGroupId(ProductGroupId)
                Do While dr.Read
                    fgi.FeatureGroup = Convert.ToString(dr("FeatureGroup"))
                    fgi.FeatureGroupId = CInt(dr("FeatureGroupId"))
                    fgi.SortOrder = CInt(dr("SortOrder"))
                    list.Insert((fgi.SortOrder - 1), fgi)
                    fgi = New FeatureGroupInfo
                Loop
                dr.Close()
                If (list.Count = 0) Then
                    output = New StringBuilder
                    output.Append(GetCompareResource("NoFGs.Text"))
                    Return output
                End If

                Dim i As Integer = 0
                Do While (i <= (list.Count - 1))
                    output.Append("<tr class=""apc_fg_row"">")
                    output.Append(("<td class=""apc_fg_cell"" colspan=""" & Convert.ToString(col_count) & """>"))
                    output.Append("<span class=""apc_fg"">")
                    output.Append(CType(list.Item(i), FeatureGroupInfo).FeatureGroup)
                    output.Append("</span>")
                    output.Append("</td>")
                    output.Append("</tr>")
                    dr = Nothing
                    dr = pc.GetProductFeatures(CType(list.Item(i), FeatureGroupInfo).FeatureGroupId)
                    temp_count = 1
                    Do While dr.Read
                        Dim alt_param As String
                        Dim pic_path As String
                        Dim js_params As String
                        If (temp_count = 1) Then
                            output.Append("<tr class=""apc_feature_row"">")
                            output.Append("<td class=""apc_feature_cell"">")
                            output.Append("<span class=""apc_feature"">")
                            output.Append(Convert.ToString(dr("Feature")))
                            output.Append("</span>")
                            If (Strings.Len(Convert.ToString(dr("Comment"))) > 0) Then
                                output.Append(String.Concat(New String() {"<div style=""display: inline;""><img id=""ac_", Convert.ToString((dr("FeatureId"))), """ onmouseover=""ac_togglePreview(this, ", Convert.ToString(dr("FeatureId")), ")"" src=""", AbsPath, "/img/about.gif"" /></div>"}))
                            End If
                            output.Append("</td>")
                        End If
                        Dim left As String = Convert.ToString((dr("SupportValue")))
                        If left.Equals("1") Then
                            pic_path = (AbsPath & "img/warning.gif")
                            alt_param = GetLegendResource("NAClick.Text")
                            js_params = (Convert.ToString((dr("ProductId"))) & "|" & Convert.ToString((dr("FeatureId"))) & "|2")
                        ElseIf left.Equals("2") Then
                            pic_path = (AbsPath & "img/unsupported.gif")
                            alt_param = GetLegendResource("UnsupportedClick.Text")
                            js_params = (Convert.ToString((dr("ProductId"))) & "|" & Convert.ToString((dr("FeatureId"))) & "|3")
                        ElseIf left.Equals("3") Then
                            pic_path = (AbsPath & "img/partial.gif")
                            alt_param = GetLegendResource("PartialSupportClick.Text")
                            js_params = (Convert.ToString((dr("ProductId"))) & "|" & Convert.ToString((dr("FeatureId"))) & "|4")
                        ElseIf left.Equals("4") Then
                            pic_path = (AbsPath & "img/supported.gif")
                            alt_param = GetLegendResource("SupportedClick.Text")
                            js_params = (Convert.ToString((dr("ProductId"))) & "|" & Convert.ToString((dr("FeatureId"))) & "|1")
                        Else
                            pic_path = (AbsPath & "img/warning.gif")
                            alt_param = GetLegendResource("NAClick.Text")
                            js_params = (Convert.ToString((dr("ProductId"))) & "|" & Convert.ToString((dr("FeatureId"))) & "|2")
                        End If
                        output.Append("<td class=""apc_support_value_cell""><span class=""apc_support_value"">")
                        output.Append(String.Concat(New String() {"<img alt=""", alt_param, """ src=""", pic_path, """ onClick=""javascript:apc_UpdateSupportValue('", js_params, "');""></img>"}))
                        output.Append("</span>")
                        output.Append("</td>")
                        temp_count += 1
                        If (temp_count = col_count) Then
                            output.Append("</tr>")
                            temp_count = 1
                        End If
                    Loop
                    dr.Close()
                    dr = Nothing
                    i += 1
                Loop
                'Dim settings As Hashtable = DotNetNuke.Entities.Portals.PortalSettings.GetModuleSettings(intModuleId)
                'If CBool(Me.Settings.Item("Pricing")) Then
                dr = pc.GetProductListByProductGroupId(ProductGroupId)
                output.Append("<tr class=""apc_pricing_row"">")
                output.Append("<td class=""apc_price_head_cell""><span class=""apc_price_head"">" & GetCompareResource("Pricing.Text") & "</span></td>")
                Do While dr.Read
                    output.Append("<td class=""apc_price_cell"">")
                    output.Append("<span class=""apc_price"">")
                    output.Append(Convert.ToDecimal((dr("Price"))).ToString("c"))
                    output.Append("</span>")
                    output.Append("</td>")
                    output.Append("</tr>")
                Loop
                dr.Close()
                dr.Dispose()
                'End If
                With output
                    .Append("</table>")
                    .Append("<br />")
                    .Append("<table class=""apc_legend_table "" cellpadding=""0"" cellspacing=""0"">")
                    .Append("<tr class=""apc_legend_head_row""><td class=""apc_legend_head_cell""><span class=""apc_legend_head"">" & GetLegendResource("Legend.Text") & "</span></td>")
                    .Append("<tr class=""apc_legend_row""><td class=""apc_legend_cell""><span class=""apc_legend"">")
                    .Append(("<img alt=""" & GetLegendResource("FeatureComment.Text") & """ src=""" & AbsPath & "img/about.gif""></img> = " & GetLegendResource("FCMessage.Text") & "<br />"))
                    .Append(("<img alt=""" & GetLegendResource("Unsupported.Text") & """ src=""" & AbsPath & "img/unsupported.gif""></img> = " & GetLegendResource("Unsupported.Text") & " <br />"))
                    .Append(("<img alt=""" & GetLegendResource("PartialSupport.Text") & """ src=""" & AbsPath & "img/partial.gif""></img> = " & GetLegendResource("PartialSupport.Text") & "<br />"))
                    .Append(("<img alt=""" & GetLegendResource("Supported.Text") & """ src=""" & AbsPath & "img/supported.gif""></img> = " & GetLegendResource("Supported.Text") & "<br />"))
                    .Append(("<img alt=""" & GetLegendResource("NA.Text") & """ src=""" & AbsPath & "img/warning.gif""></img> = " & GetLegendResource("NA.Text") & "<br />"))
                    .Append("</span></td></tr></table>")
                End With

                Return output
            End If
        Catch ex As Exception
            output = New StringBuilder
            output.Append(GetCompareResource("Error.Text"))
            Return output
        End Try
        output = New StringBuilder
        output.Append(GetCompareResource("NoProducts.Text"))
        Return output
    End Function

    <System.Web.Script.Services.ScriptMethod(), System.Web.Services.WebMethod()> _
    Public Shared Function GetFeatureComment(ByVal FeatureId As String) As String
        Try
            Dim feature As New FeatureInfo
            feature = New FeatureController().GetFeature(CInt(FeatureId))
            If (Strings.Len(feature.Comment) > 0) Then
                Return feature.Comment
            End If
            Return "<span class=""NormalRed"">There was an error loading your comparison.</span>"
        Catch ex As Exception
            Return "<span class=""NormalRed"">There was an error loading your comparison.</span>"
        End Try
    End Function

    <System.Web.Script.Services.ScriptMethod(), System.Web.Services.WebMethod()> _
    Public Shared Function UpdateSupportValue_NoRepaint(ByVal ProductGroupId As String, ByVal SupportValues As String, ByVal ModuleId As String, ByVal TabId As String, ByVal AbsPath As String) As String
        If Current.Session("CanEdit") = Current.Session.SessionID Then
            Try
                Dim textArray As String() = SupportValues.Split(New Char() {"|"c})
                If textArray(0).ToString.Equals(String.Empty) Or _
                textArray(1).ToString.Equals(String.Empty) Or _
                textArray(2).ToString.Equals(String.Empty) Then
                    Return ACAjaxHelper.BuildComparison(CInt(ProductGroupId), AbsPath).ToString
                End If
                Dim productId As Integer = CInt(textArray(0))
                Dim featureId As Integer = CInt(textArray(1))
                Dim supportValue As Integer = CInt(textArray(2))
                Dim controller As New ProductController
                Dim alt_param As String
                Dim pic_path As String
                Dim js_params As String
                Dim img_id As String

                If controller.SaveProductFeature(productId, featureId, supportValue) Then
                    Select Case supportValue
                        Case 1
                            pic_path = (AbsPath & "img/warning.gif")
                            alt_param = GetLegendResource("NAClick.Text")
                            js_params = productId & "|" & featureId & "|2"
                        Case 2
                            pic_path = (AbsPath & "img/unsupported.gif")
                            alt_param = GetLegendResource("UnsupportedClick.Text")
                            js_params = productId & "|" & featureId & "|3"
                        Case 3
                            pic_path = (AbsPath & "img/partial.gif")
                            alt_param = GetLegendResource("PartialSupportClick.Text")
                            js_params = productId & "|" & featureId & "|4"
                        Case 4
                            pic_path = (AbsPath & "img/supported.gif")
                            alt_param = GetLegendResource("SupportedClick.Text")
                            js_params = productId & "|" & featureId & "|1"
                        Case Else
                            pic_path = (AbsPath & "img/warning.gif")
                            alt_param = GetLegendResource("NAClick.Text")
                            js_params = productId & "|" & featureId & "|2"
                    End Select
                    img_id = "acimg_" & js_params
                    Return img_id & "#" & alt_param & "#" & pic_path
                    '& "#" & js_params & "#acsp_" & productId & "|" & featureId & "|" & supportValue
                End If
                pic_path = (AbsPath & "img/warning.gif")
                alt_param = GetLegendResource("NAClick.Text")
                js_params = productId & "|" & featureId & "|2"
                img_id = "acimg_" & js_params
                Return img_id & "#" & alt_param & "#" & pic_path
            Catch ex As Exception
                Return ""
            End Try
        End If
        Return ""
    End Function
    <System.Web.Script.Services.ScriptMethod(), System.Web.Services.WebMethod()> _
    Public Shared Function UpdateSupportValue(ByVal ProductGroupId As String, ByVal SupportValues As String, ByVal ModuleId As String, ByVal TabId As String, ByVal AbsPath As String) As String
        If Current.Session("CanEdit") = Current.Session.SessionID Then
            Try
                Dim textArray As String() = SupportValues.Split(New Char() {"|"c})
                If textArray(0).ToString.Equals(String.Empty) Or _
                textArray(1).ToString.Equals(String.Empty) Or _
                textArray(2).ToString.Equals(String.Empty) Then
                    Return ACAjaxHelper.BuildComparison(CInt(ProductGroupId), AbsPath).ToString
                End If
                Dim productId As Integer = CInt(textArray(0))
                Dim featureId As Integer = CInt(textArray(1))
                Dim supportValue As Integer = CInt(textArray(2))
                Dim controller As New ProductController
                If controller.SaveProductFeature(productId, featureId, supportValue) Then
                    Return ACAjaxHelper.BuildComparison(CInt(ProductGroupId), AbsPath).ToString
                End If
                Return "<span class=""NormalRed"">There was an error loading your comparison.</span>"
            Catch ex As Exception
                Return "<span class=""NormalRed"">There was an error loading your comparison.</span>"
            End Try
        End If
        Return "<span class=""NormalRed"">You do not have the proper Edit permissions.</span>"
    End Function
End Class

